//
//  DemoArraySort.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2020/1/8.
//  Copyright © 2020 lovivid. All rights reserved.
//

import UIKit

class DemoArraySort: NSObject {
    class func demoLogic() {
        
        let month = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
        ];
        
        print("month=\(month)")
        
        let monthSorted = month.sorted()
        for (index, mName) in monthSorted.enumerated() {
            print("monthSorted, index:\(index) mName=\(mName)");
        }
        
        // 按第二个字母升序
        let monthSortedByBlock = month.sorted { (str1, str2) -> Bool in
            let index = str1.index(after:str1.startIndex)
            return str1[index] <= str2[index]
        }
        print("按第二个字母升序 monthSortedByBlock=\(monthSortedByBlock)")
        
        // 按第三个字母降序
        let monthSortedByBlock2 = month.sorted { (str1, str2) -> Bool in
            let index = str1.index(str1.startIndex, offsetBy: 2)
            return str1[index] > str2[index]
        }
        print("按第三个字母降序 monthSortedByBlock2=\(monthSortedByBlock2)")
        
        var monthCopy = month;
        monthCopy.sort();
        print("默认的升序，改自己，monthCopy=\(monthCopy)")
        monthCopy.reverse()
        print("反序，改自己，monthCopy=\(monthCopy)")
        
/*
输出:
         
month=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
monthSorted, index:0 mName=April
monthSorted, index:1 mName=August
monthSorted, index:2 mName=December
monthSorted, index:3 mName=February
monthSorted, index:4 mName=January
monthSorted, index:5 mName=July
monthSorted, index:6 mName=June
monthSorted, index:7 mName=March
monthSorted, index:8 mName=May
monthSorted, index:9 mName=November
monthSorted, index:10 mName=October
monthSorted, index:11 mName=September
按第二个字母升序 monthSortedByBlock=["May", "March", "January", "October", "December", "September", "February", "November", "April", "August", "July", "June"]
按第三个字母降序 monthSortedByBlock2=["May", "November", "October", "March", "April", "September", "January", "June", "July", "August", "December", "February"]
默认的升序，改自己，monthCopy=["April", "August", "December", "February", "January", "July", "June", "March", "May", "November", "October", "September"]
反序，改自己，monthCopy=["September", "October", "November", "May", "March", "June", "July", "January", "February", "December", "August", "April"]
*/
    }
}
